

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Blue~u~u~u">
  <meta name="author" content="Blue~u~u~u">
  <meta name="keywords" content="">
  <meta name="description" content="阿里云存储OSS一、开通“对象存储OSS”服务为了解决海量数据存储与弹性扩容，项目中我们采用云存储的解决方案- 阿里云OSS。  1、开通“对象存储OSS”服务  2、进入管理控制台 二、控制台使用1、创建Bucket命名：srb-file 读写权限：公共读 2、上传默认头像创建文件夹avatar，上传默认的用户头像  三、使用RAM子用户1、进入子用户管理页面  2、添加用户 3、获取子用户ke">
<meta property="og:type" content="article">
<meta property="og:title" content="阿里云存储OSS学习笔记及入门案例">
<meta property="og:url" content="http://www.slx.blue/2022/02/11/aliOss/index.html">
<meta property="og:site_name" content="Blue~u~u~u~u">
<meta property="og:description" content="阿里云存储OSS一、开通“对象存储OSS”服务为了解决海量数据存储与弹性扩容，项目中我们采用云存储的解决方案- 阿里云OSS。  1、开通“对象存储OSS”服务  2、进入管理控制台 二、控制台使用1、创建Bucket命名：srb-file 读写权限：公共读 2、上传默认头像创建文件夹avatar，上传默认的用户头像  三、使用RAM子用户1、进入子用户管理页面  2、添加用户 3、获取子用户ke">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/1.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/2.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/3.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/4.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/5.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/6.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/7.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/8.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/9.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/10.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/11.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/12.jpg">
<meta property="og:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/13.jpg">
<meta property="article:published_time" content="2022-02-11T13:52:26.000Z">
<meta property="article:modified_time" content="2022-02-11T13:53:18.827Z">
<meta property="article:author" content="Blue~u~u~u">
<meta property="article:tag" content="OSS">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/1.jpg">
  
  <title>阿里云存储OSS学习笔记及入门案例 - Blue~u~u~u~u</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.slx.blue","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":180,"cursorChar":"_","loop":true},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>

<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"/>
<script src="/live2d-widget/autoload.js"></script>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Blue~u~u</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/images/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="阿里云存储OSS学习笔记及入门案例">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-02-11 21:52" pubdate>
        2022年2月11日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      7.7k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      24 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">阿里云存储OSS学习笔记及入门案例</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：4 个月前
                
              </p>
            
            <div class="markdown-body">
              <h1 id="阿里云存储OSS"><a href="#阿里云存储OSS" class="headerlink" title="阿里云存储OSS"></a>阿里云存储OSS</h1><h2 id="一、开通“对象存储OSS”服务"><a href="#一、开通“对象存储OSS”服务" class="headerlink" title="一、开通“对象存储OSS”服务"></a>一、开通“对象存储OSS”服务</h2><p>为了解决海量数据存储与弹性扩容，项目中我们采用云存储的解决方案- 阿里云OSS。 </p>
<h3 id="1、开通“对象存储OSS”服务"><a href="#1、开通“对象存储OSS”服务" class="headerlink" title="1、开通“对象存储OSS”服务"></a>1、开通“对象存储OSS”服务</h3><p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/1.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/2.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<h3 id="2、进入管理控制台"><a href="#2、进入管理控制台" class="headerlink" title="2、进入管理控制台"></a>2、进入管理控制台</h3><p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/3.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<h2 id="二、控制台使用"><a href="#二、控制台使用" class="headerlink" title="二、控制台使用"></a>二、控制台使用</h2><h3 id="1、创建Bucket"><a href="#1、创建Bucket" class="headerlink" title="1、创建Bucket"></a>1、创建Bucket</h3><p>命名：srb-file</p>
<p>读写权限：公共读</p>
<h3 id="2、上传默认头像"><a href="#2、上传默认头像" class="headerlink" title="2、上传默认头像"></a>2、上传默认头像</h3><p>创建文件夹avatar，上传默认的用户头像</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/4.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<h2 id="三、使用RAM子用户"><a href="#三、使用RAM子用户" class="headerlink" title="三、使用RAM子用户"></a>三、使用RAM子用户</h2><h3 id="1、进入子用户管理页面"><a href="#1、进入子用户管理页面" class="headerlink" title="1、进入子用户管理页面"></a>1、进入子用户管理页面</h3><p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/5.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/6.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<h3 id="2、添加用户"><a href="#2、添加用户" class="headerlink" title="2、添加用户"></a>2、添加用户</h3><p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/7.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<h3 id="3、获取子用户key"><a href="#3、获取子用户key" class="headerlink" title="3、获取子用户key"></a>3、获取子用户key</h3><p>AccessKeyId, AccessKeySecret</p>
<h3 id="4、设置用户权限"><a href="#4、设置用户权限" class="headerlink" title="4、设置用户权限"></a>4、设置用户权限</h3><p>AliyunOSSFullAccess</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/8.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<h1 id="使用SDK访问OSS"><a href="#使用SDK访问OSS" class="headerlink" title="使用SDK访问OSS"></a>使用SDK访问OSS</h1><h2 id="一、使用SDK"><a href="#一、使用SDK" class="headerlink" title="一、使用SDK"></a>一、使用SDK</h2><p>在OSS的概览页右下角找到“Bucket管理”，点击“OSS学习路径”</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/9.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<p>点击“Java SDK”进入SDK开发文档</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/10.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<h2 id="二、创建测试项目"><a href="#二、创建测试项目" class="headerlink" title="二、创建测试项目"></a>二、创建测试项目</h2><h3 id="1、创建Maven项目"><a href="#1、创建Maven项目" class="headerlink" title="1、创建Maven项目"></a>1、创建Maven项目</h3><p>com.atguigu</p>
<p>aliyun-oss</p>
<h3 id="2、配置pom"><a href="#2、配置pom" class="headerlink" title="2、配置pom"></a>2、配置pom</h3><p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/11.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--aliyunOSS--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.aliyun.oss<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>aliyun-sdk-oss<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.10.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>4.12<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="三、测试用例"><a href="#三、测试用例" class="headerlink" title="三、测试用例"></a>三、测试用例</h2><h3 id="1、创建Bucket-1"><a href="#1、创建Bucket-1" class="headerlink" title="1、创建Bucket"></a>1、创建Bucket</h3><p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/12.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.aliyunoss;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">OSSTest</span> </span>&#123;<br>    <span class="hljs-comment">// Endpoint以杭州为例，其它Region请按实际情况填写。</span><br>    String endpoint = <span class="hljs-string">&quot;your endpoint&quot;</span>;<br>    <span class="hljs-comment">// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。</span><br>    String accessKeyId = <span class="hljs-string">&quot;your accessKeyId&quot;</span>;<br>    String accessKeySecret = <span class="hljs-string">&quot;your accessKeySecret&quot;</span>;<br>    String bucketName = <span class="hljs-string">&quot;srb-file&quot;</span>;<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testCreateBucket</span><span class="hljs-params">()</span> </span>&#123;<br>        <br>        <span class="hljs-comment">// 创建OSSClient实例。</span><br>        OSS ossClient = <span class="hljs-keyword">new</span> OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);<br>        <span class="hljs-comment">// 创建存储空间。</span><br>        ossClient.createBucket(bucketName);<br>        <span class="hljs-comment">// 关闭OSSClient。</span><br>        ossClient.shutdown();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>2、判断bucket是否存在</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testExist</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-comment">// 创建OSSClient实例。</span><br>    OSS ossClient = <span class="hljs-keyword">new</span> OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);<br>    <span class="hljs-keyword">boolean</span> exists = ossClient.doesBucketExist(bucketName);<br>    System.out.println(exists);<br>    <span class="hljs-comment">// 关闭OSSClient。</span><br>    ossClient.shutdown();<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="3、设置bucket访问权限"><a href="#3、设置bucket访问权限" class="headerlink" title="3、设置bucket访问权限"></a>3、设置bucket访问权限</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Test</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testAccessControl</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-comment">// 创建OSSClient实例。</span><br>    OSS ossClient = <span class="hljs-keyword">new</span> OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);<br>    <span class="hljs-comment">// 设置存储空间的访问权限为：公共读。</span><br>    ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);<br>    <span class="hljs-comment">// 关闭OSSClient。</span><br>    ossClient.shutdown();<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="创建OSS微服务"><a href="#创建OSS微服务" class="headerlink" title="创建OSS微服务"></a>创建OSS微服务</h1><h2 id="一、新建云存储微服务"><a href="#一、新建云存储微服务" class="headerlink" title="一、新建云存储微服务"></a>一、新建云存储微服务</h2><h3 id="1、创建模块"><a href="#1、创建模块" class="headerlink" title="1、创建模块"></a>1、创建模块</h3><p>service-oss</p>
<h3 id="2、配置pom-xml"><a href="#2、配置pom-xml" class="headerlink" title="2、配置pom.xml"></a>2、配置pom.xml</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.atguigu<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>service-base<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!--lombok用来简化实体类：需要安装lombok插件--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--aliyunOSS--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.aliyun.oss<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>aliyun-sdk-oss<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 日期工具栏依赖 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>joda-time<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>joda-time<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--让自定义的配置在application.yaml进行自动提示--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-configuration-processor<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">optional</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">optional</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="3、配置application-yml"><a href="#3、配置application-yml" class="headerlink" title="3、配置application.yml"></a>3、配置application.yml</h3><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">server:</span><br>  <span class="hljs-attr">port:</span> <span class="hljs-number">8130</span> <span class="hljs-comment"># 服务端口</span><br><span class="hljs-attr">spring:</span><br>  <span class="hljs-attr">profiles:</span><br>    <span class="hljs-attr">active:</span> <span class="hljs-string">dev</span> <span class="hljs-comment"># 环境设置</span><br>  <span class="hljs-attr">application:</span><br>    <span class="hljs-attr">name:</span> <span class="hljs-string">service-oss</span> <span class="hljs-comment"># 服务名</span><br><span class="hljs-attr">aliyun:</span><br>  <span class="hljs-attr">oss:</span><br>    <span class="hljs-attr">endpoint:</span> <span class="hljs-string">你的endponit</span><br>    <span class="hljs-attr">keyId:</span> <span class="hljs-string">你的阿里云keyid</span><br>    <span class="hljs-attr">keySecret:</span> <span class="hljs-string">你的阿里云keysecret</span><br>    <span class="hljs-attr">bucketName:</span> <span class="hljs-string">srb-file</span><br></code></pre></td></tr></table></figure>

<h3 id="4、logback-spring-xml"><a href="#4、logback-spring-xml" class="headerlink" title="4、logback-spring.xml"></a>4、logback-spring.xml</h3><p>修改日志路径为 srb_log/oss</p>
<h3 id="5、创建启动类"><a href="#5、创建启动类" class="headerlink" title="5、创建启动类"></a>5、创建启动类</h3><p>创建ServiceOssApplication.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.srb.oss;<br><span class="hljs-meta">@SpringBootApplication</span><br><span class="hljs-meta">@ComponentScan(&#123;&quot;com.atguigu.srb&quot;, &quot;com.atguigu.common&quot;&#125;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ServiceOssApplication</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        SpringApplication.run(ServiceOssApplication.class, args);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="6、启动项目"><a href="#6、启动项目" class="headerlink" title="6、启动项目"></a>6、启动项目</h3><h2 id="二、实现文件上传"><a href="#二、实现文件上传" class="headerlink" title="二、实现文件上传"></a>二、实现文件上传</h2><h3 id="1、从配置文件读取常量"><a href="#1、从配置文件读取常量" class="headerlink" title="1、从配置文件读取常量"></a>1、从配置文件读取常量</h3><p>创建常量读取工具类：OssProperties.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.srb.oss.util;<br><span class="hljs-meta">@Setter</span><br><span class="hljs-meta">@Getter</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-meta">@ConfigurationProperties(prefix = &quot;aliyun.oss&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">OssProperties</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">InitializingBean</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String endpoint;<br>    <span class="hljs-keyword">private</span> String keyId;<br>    <span class="hljs-keyword">private</span> String keySecret;<br>    <span class="hljs-keyword">private</span> String bucketName;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String ENDPOINT;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String KEY_ID;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String KEY_SECRET;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String BUCKET_NAME;<br>    <span class="hljs-comment">//当私有成员被赋值后，此方法自动被调用，从而初始化常量</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">afterPropertiesSet</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> Exception </span>&#123;<br>        ENDPOINT = endpoint;<br>        KEY_ID = keyId;<br>        KEY_SECRET = keySecret;<br>        BUCKET_NAME = bucketName;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="2、文件上传业务"><a href="#2、文件上传业务" class="headerlink" title="2、文件上传业务"></a>2、文件上传业务</h3><p>创建Service接口：FileService.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.srb.oss.service;<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">FileService</span> </span>&#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 文件上传至阿里云</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function">String <span class="hljs-title">upload</span><span class="hljs-params">(InputStream inputStream, String <span class="hljs-keyword">module</span>, String fileName)</span></span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>实现：FileServiceImpl.java</p>
<p>参考SDK中的：Java-&gt;上传文件-&gt;简单上传-&gt;流式上传-&gt;上传文件流</p>
<p><img src="https://lllong.oss-cn-shenzhen.aliyuncs.com/AliOSS/%E9%98%BF%E9%87%8C%E4%BA%91OSS/13.jpg" srcset="/img/loading.gif" lazyload alt="img"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.srb.oss.service.impl;<br><span class="hljs-meta">@Service</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FileServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">FileService</span> </span>&#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 文件上传至阿里云</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">upload</span><span class="hljs-params">(InputStream inputStream, String <span class="hljs-keyword">module</span>, String fileName)</span> </span>&#123;<br>        <span class="hljs-comment">// 创建OSSClient实例。</span><br>        OSS ossClient = <span class="hljs-keyword">new</span> OSSClientBuilder().build(<br>                OssProperties.ENDPOINT,<br>                OssProperties.KEY_ID,<br>                OssProperties.KEY_SECRET);<br>        <span class="hljs-comment">//判断oss实例是否存在：如果不存在则创建，如果存在则获取</span><br>        <span class="hljs-keyword">if</span>(!ossClient.doesBucketExist(OssProperties.BUCKET_NAME))&#123;<br>            <span class="hljs-comment">//创建bucket</span><br>            ossClient.createBucket(OssProperties.BUCKET_NAME);<br>            <span class="hljs-comment">//设置oss实例的访问权限：公共读</span><br>            ossClient.setBucketAcl(OssProperties.BUCKET_NAME, CannedAccessControlList.PublicRead);<br>        &#125;<br>        <span class="hljs-comment">//构建日期路径：avatar/2019/02/26/文件名</span><br>        String folder = <span class="hljs-keyword">new</span> DateTime().toString(<span class="hljs-string">&quot;yyyy/MM/dd&quot;</span>);<br>        <span class="hljs-comment">//文件名：uuid.扩展名</span><br>        fileName = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf(<span class="hljs-string">&quot;.&quot;</span>));<br>        <span class="hljs-comment">//文件根路径</span><br>        String key = <span class="hljs-keyword">module</span> + <span class="hljs-string">&quot;/&quot;</span> + folder + <span class="hljs-string">&quot;/&quot;</span> + fileName;<br>        <span class="hljs-comment">//文件上传至阿里云</span><br>        ossClient.putObject(OssProperties.BUCKET_NAME, key, inputStream);<br>        <span class="hljs-comment">// 关闭OSSClient。</span><br>        ossClient.shutdown();<br>        <span class="hljs-comment">//阿里云文件绝对路径</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;https://&quot;</span> + OssProperties.BUCKET_NAME + <span class="hljs-string">&quot;.&quot;</span> + OssProperties.ENDPOINT + <span class="hljs-string">&quot;/&quot;</span> + key;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="3、控制层"><a href="#3、控制层" class="headerlink" title="3、控制层"></a>3、控制层</h3><p>创建controller.admin：FileController.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.srb.oss.controller.api;<br><span class="hljs-meta">@Api(tags = &quot;阿里云文件管理&quot;)</span><br><span class="hljs-meta">@CrossOrigin</span> <span class="hljs-comment">//跨域</span><br><span class="hljs-meta">@RestController</span><br><span class="hljs-meta">@RequestMapping(&quot;/api/oss/file&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FileController</span> </span>&#123;<br>    <span class="hljs-meta">@Resource</span><br>    <span class="hljs-keyword">private</span> FileService fileService;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 文件上传</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@ApiOperation(&quot;文件上传&quot;)</span><br>    <span class="hljs-meta">@PostMapping(&quot;/upload&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> R <span class="hljs-title">upload</span><span class="hljs-params">(</span></span><br><span class="hljs-params"><span class="hljs-function">            <span class="hljs-meta">@ApiParam(value = &quot;文件&quot;, required = true)</span></span></span><br><span class="hljs-params"><span class="hljs-function">            <span class="hljs-meta">@RequestParam(&quot;file&quot;)</span> MultipartFile file,</span></span><br><span class="hljs-params"><span class="hljs-function">            <span class="hljs-meta">@ApiParam(value = &quot;模块&quot;, required = true)</span></span></span><br><span class="hljs-params"><span class="hljs-function">            <span class="hljs-meta">@RequestParam(&quot;module&quot;)</span> String <span class="hljs-keyword">module</span>)</span>  </span>&#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            InputStream inputStream = file.getInputStream();<br>            String originalFilename = file.getOriginalFilename();<br>            String uploadUrl = fileService.upload(inputStream, <span class="hljs-keyword">module</span>, originalFilename);<br>            <span class="hljs-comment">//返回r对象</span><br>            <span class="hljs-keyword">return</span> R.ok().message(<span class="hljs-string">&quot;文件上传成功&quot;</span>).data(<span class="hljs-string">&quot;url&quot;</span>, uploadUrl);<br>        &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> BusinessException(ResponseEnum.UPLOAD_ERROR, e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="4、Swagger测试"><a href="#4、Swagger测试" class="headerlink" title="4、Swagger测试"></a>4、Swagger测试</h3><h2 id="三、实现文件删除"><a href="#三、实现文件删除" class="headerlink" title="三、实现文件删除"></a>三、实现文件删除</h2><h3 id="1、业务层"><a href="#1、业务层" class="headerlink" title="1、业务层"></a>1、业务层</h3><p>Service接口：FileService.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 根据路径删除文件</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> url</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">removeFile</span><span class="hljs-params">(String url)</span></span>;<br></code></pre></td></tr></table></figure>

<p>实现：FileServiceImpl.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 根据路径删除文件</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> url</span><br><span class="hljs-comment">     */</span><br> <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeFile</span><span class="hljs-params">(String url)</span> </span>&#123;<br>        <span class="hljs-comment">// 创建OSSClient实例。</span><br>        OSS ossClient = <span class="hljs-keyword">new</span> OSSClientBuilder().build(<br>                OssProperties.ENDPOINT,<br>                OssProperties.KEY_ID,<br>                OssProperties.KEY_SECRET);<br>        <span class="hljs-keyword">int</span> i = url.indexOf(<span class="hljs-string">&quot;.com/&quot;</span>);<br>        String substring = url.substring(i + <span class="hljs-number">5</span>);<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">//若存储的url带有中文，需要解码不然无法删除</span><br>            substring = URLDecoder.decode(substring, <span class="hljs-string">&quot;utf-8&quot;</span>);<br>        &#125; <span class="hljs-keyword">catch</span> (UnsupportedEncodingException e) &#123;<br>            e.printStackTrace();<br>        &#125;<br>        <span class="hljs-comment">// 删除文件。</span><br>        ossClient.deleteObject(OssProperties.BUCKET_NAME, substring);<br><br>        <span class="hljs-comment">// 关闭OSSClient。</span><br>        ossClient.shutdown();<br>    &#125;<br></code></pre></td></tr></table></figure>

<h3 id="2、控制层"><a href="#2、控制层" class="headerlink" title="2、控制层"></a>2、控制层</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@ApiOperation(&quot;删除OSS文件&quot;)</span><br><span class="hljs-meta">@DeleteMapping(&quot;/remove&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> R <span class="hljs-title">remove</span><span class="hljs-params">(</span></span><br><span class="hljs-params"><span class="hljs-function">    <span class="hljs-meta">@ApiParam(value = &quot;要删除的文件路径&quot;, required = true)</span></span></span><br><span class="hljs-params"><span class="hljs-function">    <span class="hljs-meta">@RequestParam(&quot;url&quot;)</span> String url)</span> </span>&#123;<br>    fileService.removeFile(url);<br>    <span class="hljs-keyword">return</span> R.ok().message(<span class="hljs-string">&quot;删除成功&quot;</span>);<br>&#125;<br></code></pre></td></tr></table></figure>
            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/OSS/">OSS</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/OSS/">OSS</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客目前大部分文章都是参考尚硅谷或者马士兵教育的学习资料！<a href="http://www.atguigu.com/" rel="nofollow noopener"官网地址！</a> 
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/02/17/ComputerNetwork/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">计算机网络学习笔记</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/02/11/%E5%8D%95%E7%82%B9%E7%99%BB%E5%BD%95/">
                        <span class="hidden-mobile">单点登录学习笔记</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">

  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
	<!--《添加网站运行时间 -->
<br/>

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date(); 

function createtime() {
    //此处修改你的建站时间或者网站上线时间
    var grt = new Date('11/02/2021 21:39:00');
    now.setTime(now.getTime() + 250);
    days = (now - grt) / 1000 / 60 / 60 / 24;

    dnum = Math.floor(days);
    hours = (now - grt) / 1000 / 60 / 60 - (24 * dnum);
    hnum = Math.floor(hours);
    if (String(hnum).length == 1) {
        hnum = "0" + hnum;
    }
    minutes = (now - grt) / 1000 / 60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes);
    if (String(mnum).length == 1) {
        mnum = "0" + mnum;
    }
    seconds = (now - grt) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if (String(snum).length == 1) {
        snum = "0" + snum;
    }
    document.getElementById("timeDate").innerHTML = " 本站已各种夹缝中安全运行 " + dnum + " 天 ";
    document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()", 250);
</script>

<!-- 添加网站运行时间》-->
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
